package edu.tsinghua.lumaqq.qq.packets.in._08;

import java.nio.ByteBuffer;

import edu.tsinghua.lumaqq.qq._08QQ;
import edu.tsinghua.lumaqq.qq.beans.QQUser;
import edu.tsinghua.lumaqq.qq.packets._08BasicInPacket;
import edu.tsinghua.lumaqq.qq.packets.PacketParseException;

/**
 * 登陆密匙返回包
 * ---------------------
 * 包格式：
 * 1. 包头 1字节 0x02
 * 2. 版本号 2字节 0x113f
 * 3. 命令字 2字节 0x00dd
 * 4. 包序号 2字节 0x2679， 一直没变过
 * 5. 硕大的加密包体,密匙是QQ密码的两次MD5
 * 6. 包尾 0x03
 *
 * @author luna
 */
public class _08LoginKeyReplyPacket extends _08BasicInPacket {

    //未知的5字节数据
    byte unknown5[];
    //32字节的的未知数据1
    public byte unknown32_1[];
    //32字节的未知数据2
    public byte unknown32_2[];
    public byte loginToken[];
    public byte key[];
    public byte errorMsg[];
    public byte replyCode;

    public _08LoginKeyReplyPacket(ByteBuffer buf, int length, QQUser user)
            throws PacketParseException {
        super(buf, length, user);
        // TODO Auto-generated constructor stub
    }

    /*
     * (non-Javadoc)
     * 
     * @see edu.tsinghua.lumaqq.qq.packets._08InPacket#getPacketName()
     */
    @Override
    public String getPacketName() {
        return "Login key Reply _08Packet";
    }

    @Override
    public byte[] getDecryptKey(byte[] body) {
        return user.getPasswordKey();
    }

    @Override
    public byte[] getFallbackDecryptKey(byte[] body) {
        return user.getLoginTokenRandomKey();
    }

    @Override
    protected void parseBody(ByteBuffer buf) throws PacketParseException {
//		System.out.println("LoginReplyKey的parseBody方法已经被执行");
        buf.getChar();
        replyCode = buf.get();
        buf.getInt();
        char len = buf.getChar();
        unknown32_1 = new byte[len];
        buf.get(unknown32_1);
        len = buf.getChar();
        unknown32_2 = new byte[len];
        buf.get(unknown32_2);
        len = buf.getChar();
        switch (replyCode) {
            case _08QQ.QQ_REPLY_OK:
                loginToken = new byte[len];
                buf.get(loginToken);
                key = new byte[_08QQ.QQ_LENGTH_KEY];
                buf.get(key);
                break;
            case _08QQ.QQ_CMD_PASSWORD_ERROR:
            case _08QQ.QQ_CMD_NEED_ACTIVATE:
                errorMsg = new byte[len];
                buf.get(errorMsg);
                break;
            default:
                errorMsg = new byte[len];
                buf.get(errorMsg);
                break;

        }


    }
}
